import { createStore } from 'redux'
//实现数据持久化
let userInfo = localStorage.getItem('userInfo')
try {
    userInfo = JSON.parse(userInfo) || {}
} catch (err) {
    userInfo = {}
}
console.log('userinfo', userInfo);
//初始值
const initState = {
    userInfo,
}
const reducer = (state, { type, payload }) => {
    let newState = { ...state }
    switch (type) {
        case 'login':
            newState.userInfo = payload
            localStorage.setItem('userInfo', JSON.stringify(payload))
            console.log('newState', newState);
            return newState
        case 'logout':
            newState.userInfo = {}
            localStorage.removeItem('userInfo')
            return newState;
        case 'setpaypwd':
            newState.userInfo.paypassword = payload.paypassword
            localStorage.setItem('userInfo', JSON.stringify(newState.userInfo));
            return newState
        default:
            return newState
    }
}
// 创建全局store仓库
const store = createStore(reducer, initState)
export default store;